package cn.edu.ujn.multiple.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.multiple.model.BaseDict;
import cn.edu.ujn.multiple.model.Customer;
import cn.edu.ujn.multiple.model.User;
import cn.edu.ujn.multiple.service.IBaseDictService;
import cn.edu.ujn.multiple.service.ICustomerService;
import cn.edu.ujn.multiple.util.Page;

@Controller
public class CustomerController {

	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IBaseDictService baseDictService;

	// 模拟直接访问并填充下拉菜单
	@GetMapping("/customer")
	public String toCustomer(Model m) {
		List<BaseDict> fromType = baseDictService.findBaseDictByTypeCode("002");
		List<BaseDict> industryType = baseDictService.findBaseDictByTypeCode("001");
		List<BaseDict> levelType = baseDictService.findBaseDictByTypeCode("006");
		m.addAttribute("fromType", fromType);
		m.addAttribute("industryType", industryType);
		m.addAttribute("levelType", levelType);
		return "customer";
	}
	// 查询之后显示查询结果
	@GetMapping("/customer/list.action")
	public String query(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
			String custName, String custSource, String custIndustry, String custLevel, Model m) {
		List<BaseDict> fromType = baseDictService.findBaseDictByTypeCode("002");
		List<BaseDict> industryType = baseDictService.findBaseDictByTypeCode("001");
		List<BaseDict> levelType = baseDictService.findBaseDictByTypeCode("006");
		m.addAttribute("fromType", fromType);
		m.addAttribute("industryType", industryType);
		m.addAttribute("levelType", levelType);
		m.addAttribute("custName", custName);
		m.addAttribute("custSource", custSource);
		m.addAttribute("custIndustry", custIndustry);
		m.addAttribute("custLevel", custLevel);

		Page<Customer> list = customerService.findCustomerList(page, rows, custName, custSource, custIndustry,
				custLevel);
		m.addAttribute("page", list);

		return "customer";
	}

	// 新增顾客
	@RequestMapping("/customer/create.action")
	@ResponseBody
	public String insert(Customer customer, HttpSession session) {
		User user = (User) session.getAttribute("USER_SESSION");
		customer.setCustCreateId(user.getUserId());
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		customer.setCustCreatetime(timestamp);
		customer.setCustSource(customer.getCustSource().equals("") ? "6" : customer.getCustSource());
		customer.setCustIndustry(customer.getCustIndustry().equals("") ? "1" : customer.getCustIndustry());
		customer.setCustLevel(customer.getCustLevel().equals("") ? "22" : customer.getCustLevel());
		Customer insert = customerService.insert(customer);
		System.out.println(insert);
		if (insert != null) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	// 获取顾客信息
	@RequestMapping("/customer/getCustomerById.action")
	@ResponseBody
	public Customer getInfo(Integer id) {
		Customer customer = customerService.findByPrimaryKey(id);
		return customer;
	}

	// 更新操作
	@RequestMapping("/customer/update.action")
	@ResponseBody
	public String update(Customer customer) {
		customer.setCustSource(customer.getCustSource().equals("") ? "6" : customer.getCustSource());
		customer.setCustIndustry(customer.getCustIndustry().equals("") ? "1" : customer.getCustIndustry());
		customer.setCustLevel(customer.getCustLevel().equals("") ? "22" : customer.getCustLevel());
		Customer update = customerService.update(customer);
		if (update != null) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	// 删除顾客
	@RequestMapping("/customer/delete.action")
	@ResponseBody
	public String delete(Integer custId) {
		int i = customerService.delete(custId);
		if (i > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

}
